#include "ImageGray.h"
#include "xtprobeImageDiskXFile.h"
#include "ipSobel.h"
#include "ipCanny.h"
#include "ipRoberts.h"
#include "ipPrewitt.h"
#include "CmdLine.h"
#include "tp4.h"

 
using namespace Torch;
 
int main(int argc, char **argv)
{
 	char *image_filename;
	char *compare;
 	real threshold;
 	bool verbose;
   	bool sobel;
	bool canny;
	bool roberts;
	bool prewitt;
	int nb_reference;
	int nb_commun;
	int nb_gradient;
   	
 	CmdLine cmd;
 	cmd.setBOption("write log", false);
   	//cmd.info(help);
   	cmd.addText("\nArguments:");
   	cmd.addSCmdArg("image filename", &image_filename, "image filename");
   	cmd.addText("\nOptions:");
   	cmd.addRCmdOption("-threshold", &threshold, 10, "threshold");
   	cmd.addBCmdOption("-verbose", &verbose, false, "verbose");
	cmd.addBCmdOption("-sobel", &sobel,false, "sobel");
	cmd.addBCmdOption("-canny", &canny,false, "canny");
	cmd.addBCmdOption("-roberts", &roberts,false, "sobel");
	cmd.addBCmdOption("-prewitt", &prewitt,false, "canny");
	//l'image fournie en argument de compare est l'image de reference
	cmd.addSCmdOption("-compare",&compare," ","fichier a comparer avec l'image argument");
 	cmd.read(argc, argv);
 
	ContourImg *contourImg = new ContourImg(image_filename); 	

	if(sobel){
	contourImg->sobel((real)threshold);
	}

	if(canny)
 	{
	contourImg->canny((real)threshold);
	}
	if(roberts)
	{
	contourImg->roberts((real)threshold);
	}
	if(prewitt)
	{
	contourImg->prewitt((real)threshold);
	}
	if(!strcmp(compare," ") == 0)
	{
		
		//La methode initCompare utilise la methode changeEdge pour transformer les contours
		//de l'image de reference en blanc et le fond blanc en noir
		contourImg->initCompare(compare);		
		nb_gradient = contourImg->nuberPixelsOfColor(contourImg->image_in, 255);
		
		nb_reference = contourImg->nuberPixelsOfColor(contourImg->image_compare, 0);
		
		nb_commun = contourImg->nombreCommun(contourImg->image_compare,contourImg->image_in);
		//print(" nb gradient %d, nb reference %d, nb commun %d\n",nb_gradient,nb_reference,nb_commun);
		double f_mesure; 
		double precision;
		double rappel;
		//if( (double)nb_commun/(double)nb_reference > 1 ) {
		//	precision = 1;
	//	}
//		else{
			precision = (double)nb_commun/(double)nb_gradient;
		//}
			if(precision > 1)
				precision = 1;
		
		//if( (double)nb_commun/(double)nb_reference > 1 ){ 
		//	rappel = 1;
		//}
		//else{
			rappel = (double)nb_commun/(double)nb_reference;
			if(rappel > 1)
				rappel = 1;
		//}
		f_mesure = 2*(precision * rappel)/(precision + rappel);

		print("Comparaison : %s et %s \n",compare,image_filename);
		
		print("Nombre gradient : %lf\n",(double)nb_gradient);
		print("Nombre reference : %lf\n",(double)nb_reference);
		print("Nombre commun : %lf\n",(double)nb_commun);
		//print("precision : %lf \n",precision);
		//print("rappel : %lf \n",rappel);
		//print("f-mesure : %lf \n",f_mesure);
		print("%lf - %lf - %lf\n",precision,rappel,f_mesure); 

	}
 	return(0);
}

